<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="top-tabs">
			<view class="tab-item" :class="{ active: currentType === '' }" @tap="switchType('')">全部</view>
			<view class="tab-item" :class="{ active: currentType === 1 }" @tap="switchType(1)">待付款</view>
			<view class="tab-item" :class="{ active: currentType === -1 }" @tap="switchType(-1)">待发货</view>
			<view class="tab-item" :class="{ active: currentType === 2 }" @tap="switchType(2)">待收货</view>
			<view class="tab-item" :class="{ active: currentType === 3 }" @tap="switchType(3)">已完成</view>
			<view class="tab-item" :class="{ active: currentType === 5 }" @tap="switchType(5)">退款</view>
		</view>
		<!-- order_status  订单状态（0：代发货；,1:待支付；2：待收货；3：已完成；4：待评价；5：退款中;6:退款完成,7:已评价,8:已取消;9:退货中，10：退款完成,11:已失效） -->
		<!-- 订单列表 -->
		<view class="order-list">
			<view class="order-item" v-for="order in orderList" :key="order.id">
				<!-- 订单头部 -->
				<view class="order-header" @tap="goToOrderDetail(order.id)">
					<view class="order-num">订单号: {{ order.order_num }}</view>
					<view class="order-status" :class="{
					'status-wait-pay': order.order_status === 1,
					'status-wait-send': order.order_status === 0,
					'status-wait-receive': order.order_status === 2,
					'status-completed': order.order_status === 3,
					'status-wait-comment': order.order_status === 4,
					'status-refunding': order.order_status === 5,
					'status-refunded': order.order_status === 6 || order.order_status === 10,
					'status-canceled': order.order_status === 8,
					'status-unknown': true
				}">
						{{ getStatusText(order.order_status) }}
					</view>
				</view>
				<!-- @tap="goToGoodsDetail(goods.id)" -->
				<!-- 商品列表 -->
				<view class="goods-list" @tap="goToOrderDetail(order.id)">
					<view v-for="goods in order.order_goods_list" :key="goods.id" class="goods-item">
						<view class="goods-image">
							<image :src="goods.goods_img" mode="aspectFill"></image>
						</view>
						<view class="goods-info">
							<view class="goods-name">{{ goods.goods_name }}</view>
							<view class="goods-spec">{{ goods.goods_attr.attrs }}</view>
							<view class="goods-price">
								<text>¥{{ goods.goods_price }}</text>
								<text class="goods-count">x{{ goods.cart_num }}</text>
							</view>
						</view>
					</view>
				</view>

				<!-- 订单底部 -->
				<view class="order-footer">
					<view class="order-total">总计: ¥{{ order.pay_money }}</view>
					<view class="order-actions">
						<button v-if="order.order_status === 1" class="btn cancel-btn"
							@tap="cancelOrder(order.id)">取消订单</button>
						<button v-if="order.order_status === 1" class="btn pay-btn"
							@tap="goToPay(order.order_num)">去付款</button>
						<button v-if="order.order_status === 8" class="btn delete-btn"
							@tap="deleteOrder(order.id)">删除订单</button>
					</view>
				</view>
			</view>
		</view>

		<!-- 无数据提示 -->
		<view class="no-data" v-if="orderList.length === 0">
			<image src="/static/iconfont/no-data.png" mode="aspectFit"></image>
			<text>暂无订单数据</text>
		</view>

		<!-- 加载更多 -->
		<view class="load-more" v-if="hasMore" @tap="loadMore">
			<text>加载更多</text>
		</view>
	</view>
</template>

<script>
	import api from '../../api/request.js';

	export default {
		created() {
			// 调试信息：检查初始数据
			console.log('订单组件初始化数据:', {
				currentType: this.currentType,
				orderList: this.orderList,
				hasMore: this.hasMore
			});
		},
		data() {
			return {
				currentType: '', // 当前订单类型，空-全部，1-待付款，-1待发货，2待收货，3已完成，5退款
				orderList: [], // 订单列表数据
				currentPage: 1, // 当前页码
				hasMore: true, // 是否有更多数据
				total: 0, // 总订单数
				showDebug: false // 是否显示调试信息
			};
		},
		onLoad() {
			// this.loadOrderList();
		},
		onReachBottom() {
			if (this.hasMore) {
				this.currentPage++;
				this.loadOrderList();
			}
		},
		onShow() {
			if (!uni.getStorageSync('token')) {
				uni.redirectTo({
					url: '/pages/register/login'
				})
				return;
			}
			this.loadOrderList();
		},
		methods: {
			// 切换调试信息显示状态
			toggleDebug() {
				this.showDebug = !this.showDebug;
			},
			// 切换订单类型
			// 切换订单类型
			switchType(type) {
				this.currentType = type;
				this.currentPage = 1;
				this.orderList = [];
				this.hasMore = true;
				this.loadOrderList();
			},

			// 加载订单列表
			loadOrderList() {
				console.log('开始加载订单列表:', {
					page: this.currentPage,
					type: this.currentType
				});
				uni.showLoading({
					title: '加载中...'
				});

				api.getOrderList({
					page: this.currentPage,
					type: this.currentType
				}).then(res => {
					uni.hideLoading();
					if (res.code == 1 && res.data && res.data.rows) {
						this.total = res.data.total;

						if (this.currentPage == 1) {
							this.orderList = res.data.rows;
						} else {
							this.orderList = [...this.orderList, ...res.data.rows];
						}

						// 判断是否还有更多数据
						this.hasMore = this.orderList.length < this.total;

						// 调试信息：查看订单数据
						console.log('订单列表数据加载成功:', {
							total: this.total,
							currentPage: this.currentPage,
							orderListLength: this.orderList.length,
							hasMore: this.hasMore,
							orderList: this.orderList
						});
					} else {
						uni.showToast({
							title: res.msg || '获取订单列表失败',
							icon: 'none'
						});
					}
				}).catch(err => {
					uni.hideLoading();
					console.error('获取订单列表失败', err);
					uni.showToast({
						title: err.msg || '获取订单列表失败',
						icon: 'none'
					});
				});
			},

			// 加载更多
			// loadMore() {
			// 	if (this.hasMore) {
			// 		this.currentPage++;
			// 		this.loadOrderList();
			// 	}
			// },

			// 获取订单状态文本
			getStatusText(status) {
				switch (status) {
					case 0:
						return '待发货';
					case 1:
						return '待支付';
					case 2:
						return '待收货';
					case 3:
						return '已完成';
					case 4:
						return '待评价';
					case 5:
						return '退款中';
					case 6:
					case 10:
						return '退款完成';
					case 7:
						return '已评价';
					case 8:
						return '已取消';
					case 9:
						return '退货中';
					case 11:
						return '已失效';
					default:
						return '未知状态';
				}
			},




			// 跳转到订单详情
			goToOrderDetail(orderId) {
				uni.navigateTo({
					url: `/pages/order/orderDetail?order_id=${orderId}`
				});
			},

			// 取消订单
			cancelOrder(orderId) {
				uni.showModal({
					title: '提示',
					content: '确定要取消此订单吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '取消中...'
							});

							api.cancelOrder({
								order_id: orderId
							}).then(res => {
								uni.hideLoading();
								if (res.code === 1) {
									uni.showToast({
										title: '取消订单成功',
										icon: 'success'
									});
									// 刷新订单列表
									this.currentPage = 1;
									this.orderList = [];
									this.loadOrderList();
								} else {
									uni.showToast({
										title: res.msg || '取消订单失败',
										icon: 'none'
									});
								}
							}).catch(err => {
								uni.hideLoading();
								console.error('取消订单失败', err);
								uni.showToast({
									title: err.msg || '取消订单失败',
									icon: 'none'
								});
							});
						}
					}
				});
			},

			// 删除订单
			deleteOrder(orderId) {
				uni.showModal({
					title: '提示',
					content: '确定要删除此订单吗？',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '删除中...'
							});

							api.deleteOrder({
								order_id: orderId
							}).then(res => {
								uni.hideLoading();
								if (res.code === 1) {
									uni.showToast({
										title: '删除订单成功',
										icon: 'success'
									});
									// 刷新订单列表
									this.currentPage = 1;
									this.orderList = [];
									this.loadOrderList();
								} else {
									uni.showToast({
										title: res.msg || '删除订单失败',
										icon: 'none'
									});
								}
							}).catch(err => {
								uni.hideLoading();
								console.error('删除订单失败', err);
								uni.showToast({
									title: err.msg || '删除订单失败',
									icon: 'none'
								});
							});
						}
					}
				});
			},

			// 去付款
			goToPay(orderNum) {
				uni.navigateTo({
					url: '/pages/order/sureOrder2?order_num=' + orderNum
				});
			},

			// 跳转到商品详情
			goToGoodsDetail(goodsId) {
				uni.navigateTo({
					url: `/pages/goods/detail?goods_id=${goodsId}`
				});
			}
		}
	};
</script>

<style lang="scss">
	.container {
		padding-bottom: 60px;
	}

	.top-tabs {
		background-color: #fff;
		border-bottom: 1px solid #eee;
		padding: 10rpx;
		display: flex;
		position: sticky;
		top: 0;
		left: 0;
		z-index: 9999999999999;

		.tab-item {
			flex: 1;
			display: inline-block;
			padding: 8px 10rpx;
			border-radius: 15px;
			font-size: 14px;
			color: #666;
			text-align: center;

			&.active {
				background-color: #ff6b00;
				color: #fff;
			}
		}
	}

	.order-list {
		padding: 10px;

		.order-item {
			background-color: #fff;
			border-radius: 8px;
			padding: 15px;
			margin-bottom: 10px;
			box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);

			.order-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 10px;
				padding-bottom: 10px;
				border-bottom: 1px solid #eee;

				.order-num {
					font-size: 14px;
					color: #666;
				}

				.order-status {
					padding: 3px 8px;
					border-radius: 4px;
					font-size: 12px;
					font-weight: bold;
				}

				.status-wait-pay {
					color: #FF5252;
					background-color: #FFF0F0;
				}

				.status-wait-send {
					color: #FFA726;
					background-color: #FFF8E1;
				}

				.status-wait-receive {
					color: #2196F3;
					background-color: #E3F2FD;
				}

				.status-completed {
					color: #4CAF50;
					background-color: #E8F5E9;
				}

				.status-wait-comment {
					color: #9C27B0;
					background-color: #F3E5F5;
				}

				.status-refunding {
					color: #FF9800;
					background-color: #FFF3E0;
				}

				.status-refunded {
					color: #795548;
					background-color: #F5F5F5;
				}

				.status-canceled {
					color: #9E9E9E;
					background-color: #F5F5F5;
				}

				.status-unknown {
					color: #616161;
					background-color: #F5F5F5;
				}
			}

			.goods-list {
				margin-bottom: 10px;

				.goods-item {
					display: flex;
					padding: 10px 0;
					border-bottom: 1px solid #eee;

					&:last-child {
						border-bottom: none;
					}

					.goods-image {
						width: 80px;
						height: 80px;
						margin-right: 10px;
						overflow: hidden;
						border-radius: 4px;

						image {
							width: 100%;
							height: 100%;
						}
					}

					.goods-info {
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.goods-name {
							font-size: 14px;
							color: #333;
							line-height: 1.4;
							display: -webkit-box;
							-webkit-line-clamp: 2;
							-webkit-box-orient: vertical;
							overflow: hidden;
						}

						.goods-spec {
							font-size: 12px;
							color: #999;
						}

						.goods-price {
							display: flex;
							justify-content: space-between;
							align-items: center;

							text {
								font-size: 14px;
								color: #ff6b00;
								font-weight: bold;
							}

							.goods-count {
								font-size: 12px;
								color: #666;
								font-weight: normal;
							}
						}
					}
				}
			}

			.order-footer {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding-top: 10px;
				border-top: 1px solid #eee;

				.order-total {
					font-size: 14px;
					color: #333;

					text {
						font-weight: bold;
						color: #ff6b00;
					}
				}

				.order-actions {
					display: flex;

					.btn {
						margin-left: 10px;
						padding: 5px 15px;
						font-size: 12px;
						border-radius: 4px;
					}

					.cancel-btn {
						background-color: #f0f0f0;
						color: #666;
						border: none;
					}

					.pay-btn {
						background-color: #ff6b00;
						color: #fff;
						border: none;
					}

					.delete-btn {
						background-color: #f0f0f0;
						color: #ff6666;
						border: none;
					}
				}
			}
		}
	}

	.no-data {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 60px 0;

		image {
			width: 120px;
			height: 120px;
			margin-bottom: 20px;
		}

		text {
			font-size: 14px;
			color: #999;
		}
	}

	.load-more {
		padding: 15px 0;
		text-align: center;
		font-size: 14px;
		color: #666;
		background-color: #fff;
		margin-top: 10px;
	}
</style>